package org.jeecg.modules.wlhy.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerCompany;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: hy_customer_company签约管理
 * @Author: jeecg-boot
 * @Date:   2021-01-17
 * @Version: V1.0
 */
@Api(tags="hy_customer_company")
@RestController
@RequestMapping("/customer/hyCustomerCompany")
@Slf4j
public class HyCustomerCompanyController extends JeecgController<HyCustomerCompany, IHyCustomerCompanyService> {
	@Autowired
	private IHyCustomerCompanyService hyCustomerCompanyService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCustomerCompany
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */

	@ApiOperation(value="hy_customer_company-分页列表查询", notes="hy_customer_company-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerCompany hyCustomerCompany,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCustomerCompany> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerCompany, req.getParameterMap());

		Page<HyCustomerCompany> page = new Page<HyCustomerCompany>(pageNo, pageSize);
		IPage<HyCustomerCompany> pageList = hyCustomerCompanyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyCustomerCompany
	 * @return
	 */
	@AutoLog(value = "hy_customer_company-添加")
	@ApiOperation(value="hy_customer_company-添加", notes="hy_customer_company-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("sys:contract:add")
	public Result<?> add(@RequestBody HyCustomerCompany hyCustomerCompany) {
		String companyId = hyCustomerCompany.getCompanyId();
		String customerId = hyCustomerCompany.getCustomerId();
		if(StringUtils.isEmpty(companyId)){
			return Result.error("公司不能为空");
		}
		if(StringUtils.isEmpty(customerId)){
			return Result.error("货主不能为空");
		}

		/*int count = hyCustomerCompanyService.count(new QueryWrapper<HyCustomerCompany>()
				.eq("customer_id", customerId)
		);
		if(count > 0){
			return Result.error("不能重复添加");
		}*/

		hyCustomerCompanyService.save(hyCustomerCompany);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyCustomerCompany
	 * @return
	 */
	@AutoLog(value = "hy_customer_company-编辑")
	@ApiOperation(value="hy_customer_company-编辑", notes="hy_customer_company-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("sys:contract:edit")
	public Result<?> edit(@RequestBody HyCustomerCompany hyCustomerCompany) {
//		hyCustomerCompanyService.updateById(hyCustomerCompany);
		hyCustomerCompanyService.edit(hyCustomerCompany);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_customer_company-通过id删除")
	@ApiOperation(value="hy_customer_company-通过id删除", notes="hy_customer_company-通过id删除")
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("sys:contract:delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyCustomerCompanyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_customer_company-批量删除")
	@ApiOperation(value="hy_customer_company-批量删除", notes="hy_customer_company-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("sys:contract:deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCustomerCompanyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_customer_company-通过id查询", notes="hy_customer_company-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerCompany hyCustomerCompany = hyCustomerCompanyService.getById(id);
		if(hyCustomerCompany==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCustomerCompany);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCustomerCompany
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("sys:contract:exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HyCustomerCompany hyCustomerCompany) {
        return super.exportXls(request, hyCustomerCompany, HyCustomerCompany.class, "hy_customer_company");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@RequiresPermissions("sys:contract:importExcel")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCustomerCompany.class);
    }

}
